﻿using LDC_Shop_B.Models;
using LDC_Shop_B.Serives;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;

namespace LDC_Shop_B.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class CarController : ControllerBase
    {
        private readonly ICarService _carService;
        public CarController(ICarService carService)
        {
            //创建Redis连接对象
            _carService = carService;
        }
        /// <summary>
        /// 加入购物车 把选中的商品 加入我购物车
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public string AddCar(int goodId)
        {
            //我  登录的用户 Token中获取用户信息
            var userId = Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;
            //添加购物车
            _carService.AddCar(userId, goodId);
            return "添加成功";
        }
        /// <summary>
        ///  减少购物车 商品数量
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public string DiffCar(int goodId)
        {
            //我  登录的用户 Token中获取用户信息
            var userId = Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;
            _carService.DiffCar(userId, goodId);
            return "减少购物车数量成功";
        }

        /// <summary>
        /// 移除购物车商品
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public string RemoveCar(int goodId)
        {
            //我  登录的用户 Token中获取用户信息
            var userId = Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;
            _carService.RemoveCar(userId, goodId);
            return "移除购物车成功";
        }

        //查询购物车商品接口（需授权） 输入：无 输出：购物车商品列表（商品标题、商品图片、商品Id,购物车商品数量）购物车价格总额
        /// <summary>
        /// 查询购物车商品接口
        /// </summary>
        [Authorize]
        [HttpPost]
        public GetCarGoods GetCarGoods()
        {
            //我  登录的用户 Token中获取用户信息
            var userId = Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;
            //查询购物车商品
            var list = _carService.GetCarGoods(userId);
            //创建返回类
            var response = new GetCarGoods
            {
                //购物车商品
                CarList = list,
                //购物车的商品总额   求每一项的 价格*数量  总额
                TotalPrice = list.Sum(x => x.Price * x.Count)
            };
            return response;
        }
        /// <summary>
        /// 添加购物车 SQL
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public string AddCarForSQL(int goodId)
        {
            //我  登录的用户 Token中获取用户信息
            var userId = Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;
            var result = _carService.AddCarForSQL(Convert.ToInt32(userId), goodId);
            if(result)
                return "添加成功";
            return "添加失败";
        }
        /// <summary>
        /// 减少购物车
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public string DiffCarForSQL(int goodId)
        {
            //我  登录的用户 Token中获取用户信息
            var userId = Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;
            var result = _carService.DiffCarForSQL(Convert.ToInt32(userId),goodId);
            if (result)
                return "减少成功";
            return "减少失败";
        }
        /// <summary>
        /// 删除购物车
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public string DeleteCarForSQL(int goodId)
        {
            //我  登录的用户 Token中获取用户信息
            var userId = Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;
            var result = _carService.DeleteCarForSQL(Convert.ToInt32(userId), goodId);
            if (result)
                return "删除成功";
            return "删除失败";
        }

        /// <summary>
        /// 查询购物车
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public GetCarGoods GetCarGoodsForSQL()
        {
            var userId = Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;
            //a通过数据库查询购物车数据
           var list = _carService.GetCarGoodsForSQL(Convert.ToInt32(userId));
            //创建返回类
            var response = new GetCarGoods
            {
                //购物车商品
                CarList = list,
                //购物车的商品总额   求每一项的 价格*数量  总额
                TotalPrice = list.Sum(x => x.Price * x.Count)
            };      
            return response;
        }

    }
}
